<template>
   <div class="more-container">
     <div class="inner-content">
       <!-- 顶部表单区域 -->
    <el-row>
      <el-col :span="4">
         <el-input v-model="topForm.searchword" clearable placeholder="搜索名称/简称/管理员姓名/手机号"></el-input>
      </el-col>
    </el-row>
    <commonline :newText='"分校"' @addList='additem' @searchlist='searchlist' @resetform='resetform'></commonline>
    <!-- 新增分校弹窗 -->
    <branchschooldialog  :formType='formtype'  :isshowDialog='isshowadd'   :dialogTitle='dialogtitle' @closedialog='closedialog' @confirm='confirm'></branchschooldialog>
    <!-- 表格区域 -->
    <branchschoolTable v-if="isDataSuccess" @openedit='openedit' :isshowloading='isshowLoding' @changepageTotal='changepageTotal' @changepage='changepage' :tabData='tabList' @deleteitem='deleteitem'></branchschoolTable>
    </div>
  </div>
</template>

<script>
//引入通用行
import commonline from '@/components/commonline/commonline'
import branchschooldialog from './component/branchschooldialog'
import branchschoolTable from './component/branchschooltable.vue'
import {getbranchList,deletebranch,editbranch} from '@/api/branch'
export default {
  data(){
    return{
      //
      tableData:[],
      //数据是否请求成功
      isDataSuccess:false,
      //弹窗的title
      dialogtitle:'新增分校',
      //是否显示加载
      isshowLoding:true,
      //选中的那个tab
      activetab:1,
      //是否显示新增弹窗
      isshowadd:false,
      //顶部表单form
      topForm:{
        //关闭词搜索
        searchword:''
      },
      //表格数据
      tabList:{},
      //编辑数据
      editData:{},
      //翻页总数
      pageTotal:10,
      //第几页
      page:1,
      //新增还是编辑
      formtype:0,
      //编辑id
      editid:'',
      //总共有多少条数据
      pageTotals:'',
    }
  },
  components:{
    commonline,
    branchschooldialog,
    branchschoolTable
  },
  methods:{
    //总数翻页
    changepageTotal(val){
      this.pageTotal = val
      this.getbranchListData(this.page,this.pageTotal,this.topForm.searchword)
    },
    //翻页
    changepage(val){
      this.page = val
      this.getbranchListData(this.page,this.pageTotal,this.topForm.searchword)
    },
    //删除
    async deleteitem(id){
      let params = {
        id
      }
      //询问客户是否删除
       this.$confirm('您确认要删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let data = await deletebranch(params)
          if(data.code=='0000'){
            this.$message({
            type: 'success',
            message: '删除成功!'
          });
           //此时已经实现删除操作，所以总页数的值需要减1，Math.ceil是向上取整，保证始终大于等于1
            const totalPage = Math.ceil((this.pageTotals - 1) / 10)
            //记录当前页码
            //记住删除最后一条数据时当前码是最后一页
            const pagenum = this.page > totalPage ? totalPage : this.page
            //实现跳转
            this.page = pagenum < 1 ? 1 : pagenum
          //刷新列表
          this.getbranchListData(this.page,this.pageTotal,this.topForm.searchword)
          }
          
          
        }).catch(() => {
                
        });
    },
    //新增成功关闭弹窗
    confirm(){
      this.isDataSuccess = false
      this.isshowadd = false
      //刷新列表
      this.getbranchListData(this.page,this.pageTotal)
    },
    //获取列表数据
    async getbranchListData(page=1,rows=10){
      let params = {
        page,
        rows
      }
      if(this.topForm.searchword){
        params.keyword = this.topForm.searchword
      }
      let data = await getbranchList(params)
      if(data.code=='0000'){
         this.tabList = data.data
         this.pageTotals = data.data.page.total
      this.tableData = data.data.list
      //数据请求成功
      this.isDataSuccess = true
      this.isshowLoding = false
      }
     
    },
    //编辑
    openedit(val){
      this.$bus.emit("editform",val)
      let id = val.id
      this.dialogtitle = '编辑分校'
      this.formtype = val.type
      this.flag = true
     this.isshowadd = true
    },
    changetab(id){
      this.activetab = id
    },
    //新增分校按钮
    additem(){
      this.dialogtitle = '新增分校'
      this.formtype = 0
      this.isshowadd = true
    },
    //搜索查询
    searchlist(){
       this.$message({
            type: 'success',
            message: '查询成功!'
          });
      this.getbranchListData(this.page,this.pageTotal)
    },
    //重置
    resetform(){
      //重置表单
      this.topForm.searchword = ''
     
       this.$message({
            type: 'success',
            message: '已重置!'
          });
      this.pageTotal = 10
      this.page = 1
       //列表重置
     this.getbranchListData(this.page,this.pageTotal)
    },
    //关闭dialog
    closedialog(){
      this.flag= false
      this.isshowadd = false
    }
  },
  mounted(){
    //获取列表数据
    this.getbranchListData(this.page,this.pageTotal)
  }
}
</script>

<style scoped lang='scss'>

</style>